require "Net/send_Msg"
require "Net/guildHandel"
require 'View/UIBase/UIBase'

UIGuildRanking = UIBase.New(PanelNames.UIGuildRanking);
local rankCount = 2;

function UIGuildRanking.Init(func)
    UIBase.Init(UIGuildRanking, func);
end

--定义控件
function UIGuildRanking:OnCreate()
    self.tops = {}
    for i = 1, 1 do
        local item = self:GetChild("Top" .. i);
        table.insert(self.tops, item);
    end
    self.listInfo = {};
    self.myInfo = {};
    self.myRankCom = self:GetChild("MyRank");
    self.rankList = self:GetChild("RankList");
    self.backBtn = self:GetChild("BackButton");
    self.rankTypeController = self:GetController("c1");
    self:addEvent();
    self:RegisterEvent(Config.EventType.Union_UnionForceRank_S2C, self.setInfo);
    self:RegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo);

    --self.CloseFom();
end

function UIGuildRanking:OnEnable()
    rankCount = 2;
    guildHandel.SendUnionPowerRanks()
end

function UIGuildRanking:OnDisable()

end

function UIGuildRanking:OnDestroy()
    self:UnRegisterEvent(Config.EventType.Union_UnionForceRank_S2C, self.setInfo, self);
    self:UnRegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo);
end

function UIGuildRanking:addEvent()
    self:RegisterOnClick(self.backBtn, self.onCloseClick)
end

function UIGuildRanking:removeEvent()

end

function UIGuildRanking:onCloseClick()
    closeUI(PanelNames.UIGuildRanking);
end

function UIGuildRanking:setInfo()
    --初始化玩家个人信息
    self.listInfo = GuildManager.getGuildForceRank()
    local rankCtrl = self.myRankCom:GetController("ranking");
    local rankEmptyCtrl = self.myRankCom:GetController("empty");
    if GuildManager.getUnionId() ~= 0 then
        --自己有公会
        rankEmptyCtrl.selectedIndex = 0;
        local Rank = self.myRankCom:GetChild("Rank");
        local Name = self.myRankCom:GetChild("GuildName");
        local manNum = self.myRankCom:GetChild("manNum");
        local guildFlag = self.myRankCom:GetChild("Guildflag")
        local powerNum = self.myRankCom:GetChild("GuildCombatEffectiveness");
        --local guildLevelText = self.myRankCom:GetChild("GuildLevel");
        local rankNum = GuildManager.getMyUnionForceRank();

        if rankNum > 0 then
            if rankNum < 4 then
                rankCtrl.selectedIndex = rankNum -1;
                Rank.text = "";
            else
                rankCtrl.selectedIndex = 3;
                Rank.text = rankNum;
            end
        else
            rankCtrl.selectedIndex = 3;
            Rank.text = getLanguage("NotInRank");
        end
        Name.text = GuildManager.getUnionName().." "..string.format(getLanguage("Level"), GuildManager.getUnionLv());
        manNum.text = GuildManager.getUnionMember().."/"..GuildManager.getMaxMemberCnt(GuildManager.getUnionLv());
        guildFlag.url =  GuildManager.getFlagUrl();
        powerNum.text = HelperFunc.Format4ResourceNum(GuildManager.getUnionTotalForce());
        --guildLevelText.text = getLanguage("Sys_HeroLevel") .. GuildManager.getUnionLv();
    else
        rankEmptyCtrl.selectedIndex = 1;
        rankCtrl.selectedIndex = 3;
    end
    local rankNum = #self.listInfo - #self.tops;
    local topNum = 0;
    if rankNum > 0 then
        topNum = 5
    else
        topNum = #self.listInfo;
    end
    for i = 1, #self.tops do
        local data = self.listInfo[i];
        local item = self.tops[i];
        local controller = item:GetController("Player");
        controller.selectedIndex = 0;
        local Name = item:GetChild("GuildName");
        local guildFlag = item:GetChild("Guildflag")
        --local guildLevelText = item:GetChild("GuildLevel");
        local powerNum = item:GetChild("GuildCombatEffectiveness");
        local manNumText =  item:GetChild("manNum");
        guildFlag.url = logicMgr.GuildManager.getFlagUrl(data.flagId);

        --item:GetChild("RankHead").onClick:Clear();
        --item:GetChild("RankHead").onClick:Add(function()
        --    if tostring(data.playerShowInfo.playerId) == HeroManager.GetRoleId() then
        --        return ;
        --    end
        --    roleInfoHandler.sendreqOtherRoleInfo(data.playerShowInfo.playerId);
        --end)
        --local Level = item:GetChild("RankHead"):GetChild("level");

        --guildLevelText.text = getLanguage("Sys_HeroLevel") .. data.lv;
        Name.text = data.unionName.." "..string.format(getLanguage("Level"), data.lv);
        manNumText.text = data.memberCount.."/"..GuildManager.getMaxMemberCnt(data.lv);
        powerNum.text = HelperFunc.Format4ResourceNum(data.val);
        --Level.text = data.level;


    end
    self.rankList.itemRenderer = function(idx,obj)
        self:rendererRankItem(rankCount,obj);
    end
    self.rankList.numItems = Config.Const_Rank_Max_Num - 1;--rankNum
    HelperFunc.ListTranstionPlay(self.rankList);

end

function UIGuildRanking:GotOtherShowInfo(info)
    showUI(PanelNames.UIPlayerShow, info, nil, false, function()
        UIBottom.enterAdventure()
        showUI(PanelNames.UIGuild);
    end)
end

function UIGuildRanking:rendererRankItemNone(obj)
    obj:GetController("empty").selectedIndex = 1;
end

function UIGuildRanking:rendererRankItem(idx,obj)
    local self = UIGuildRanking;

    local data = self.listInfo[idx];
    if data == nil then
        obj:GetController("empty").selectedIndex = 1;
        return ;
    else
        obj:GetController("empty").selectedIndex = 0;
    end
    local item = obj;

    local Rank = item:GetChild("Rank");
    local Name = item:GetChild("GuildName");
    local manNum = item:GetChild("manNum");
    local guildFlag = item:GetChild("Guildflag")
    --local guildLevelText = item:GetChild("GuildLevel");
    local rankCtrl = item:GetController("ranking");
    local powerNum = item:GetChild("GuildCombatEffectiveness");

    if rankCount > 3 then
        rankCtrl.selectedIndex = 3;
    else
        rankCtrl.selectedIndex = rankCount - 1;
    end
    --headBtn.onClick:Clear();
    --headBtn.onClick:Add(function()
    --    if tostring(data.playerShowInfo.playerId) ~= logicMgr.HeroManager.GetRoleId() then
    --        roleInfoHandler.sendreqOtherRoleInfo(data.playerShowInfo.playerId);
    --    end
    --end)
    Rank.text = rankCount;
    Name.text = data.unionName.." "..string.format(getLanguage("Level"), data.lv);
    manNum.text = data.memberCount.."/"..GuildManager.getMaxMemberCnt(data.lv);
    guildFlag.url = logicMgr.GuildManager.getFlagUrl(data.flagId);
    --guildLevelText.text = string.format(getLanguage("GuildInvestment_1119"),data.lv);
    powerNum.text = HelperFunc.Format4ResourceNum(data.val);

    rankCount = rankCount + 1;
end

return UIGuildRanking;